<template>
  <div class="order-list-page">
    <!-- 顶部导航栏 -->
    <div class="header">
      <el-button type="text" @click="goBack">
        <el-icon><ArrowLeft /></el-icon>
      </el-button>
      <span class="title">商店订单</span>
      <el-button type="text" class="search-button">
        <el-icon><Search /></el-icon>
      </el-button>
    </div>
    <!-- 订单状态分类 -->
    <div class="order-status-tabs">
      <div 
        v-for="(tab, index) in orderTabs" 
        :key="index" 
        :class="{ active: activeTab === index }"
        @click="activeTab = index"
      >
        {{ tab }}
      </div>
    </div>
    <!-- 订单列表 -->
    <div class="order-items">
      <div 
        v-for="(order, orderIndex) in filteredOrders" 
        :key="orderIndex" 
        class="order-item"
      >
        <div class="order-header">
          <span>订单号：{{ order.orderId }}</span>
          <span class="status">{{ order.status }}</span>
        </div>
        <div class="order-content">
          <img :src="order.productImage" alt="商品图片">
          <div class="product-info">
            <p>{{ order.productName }}</p>
            <p>{{ order.productSpec }}</p>
          </div>
          <div class="price-info">
            <p>¥{{ order.price }}</p>
            <p>x{{ order.quantity }}</p>
          </div>
        </div>
        <div class="order-footer">
          <span>{{ order.orderTime }}</span>
          <span>金额：¥{{ order.totalAmount }}</span>
        </div>
        <div class="order-actions">
          <el-button 
            type="text" 
            v-if="order.status === '待付款'" 
            @click="cancelOrder(order.orderId)"
          >取消订单</el-button>
          <el-button 
            type="text" 
            v-if="order.status === '待付款'" 
            @click="payOrder(order.orderId)"
          >立即付款</el-button>
          <el-button 
            type="text" 
            v-if="order.status === '待收货'" 
            @click="confirmReceive(order.orderId)"
          >确定收货</el-button>
          <el-button 
            type="text" 
            v-if="order.status === '已完成'" 
            @click="deleteOrder(order.orderId)"
          >删除订单</el-button>
          <el-button 
            type="text" 
            v-if="order.status === '已取消'" 
            @click="deleteOrder(order.orderId)"
          >删除订单</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref,watch } from 'vue'
import { ArrowLeft, Search } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router' // 引入useRouter
// 模拟订单数据
const orders = ref([
  {
    orderId: '0000000001',
    status: '待付款',
    productImage: 'https://example.com/rope-image.png',
    productName: '捆绑打包编织不易断绳子',
    productSpec: '蓝色/10米',
    price: 88.00,
    quantity: 1,
    orderTime: '2022-12-12 12:12',
    totalAmount: 88.00
  },
  {
    orderId: '0000000002',
    status: '待收货',
    productImage: 'https://example.com/box-image.png',
    productName: '货物专用木质带锁箱子',
    productSpec: '米色/10方',
    price: 88.00,
    quantity: 1,
    orderTime: '2022-12-12 12:12',
    totalAmount: 88.00
  },
  {
    orderId: '0000000003',
    status: '已完成',
    productImage: 'https://example.com/rope-image.png',
    productName: '捆绑打包编织不易断绳子',
    productSpec: '蓝色/10米',
    price: 88.00,
    quantity: 1,
    orderTime: '2022-12-12 12:12',
    totalAmount: 88.00
  }
])
const orderTabs = ref(['全部', '待付款', '待收货', '已完成', '已取消'])
const activeTab = ref(0)
const router = useRouter() // 创建router实例

// 根据选中的tab过滤订单
const filteredOrders = ref([])
const goBack = () => {
  // 这里可根据实际路由情况实现返回逻辑，比如使用vue-router的router.back()
    router.back() // 使用router.back()返回上一页面

  console.log('返回上一页')
}
const cancelOrder = (orderId) => {
  console.log(`取消订单 ${orderId}`)
  // 实际项目中调用后端接口取消订单
}
const payOrder = (orderId) => {
  console.log(`支付订单 ${orderId}`)
  // 实际项目中调用后端接口进行支付
}
const confirmReceive = (orderId) => {
  console.log(`确认收货 ${orderId}`)
  // 实际项目中调用后端接口确认收货
}
const deleteOrder = (orderId) => {
  console.log(`删除订单 ${orderId}`)
  // 实际项目中调用后端接口删除订单
}
// 监听activeTab变化，过滤订单
const filterOrders = () => {
  if (activeTab.value === 0) {
    filteredOrders.value = orders.value
  } else {
    filteredOrders.value = orders.value.filter(order => order.status === orderTabs.value[activeTab.value])
  }
}
filterOrders()
const watchActiveTab = watch(activeTab, filterOrders)
</script>

<style scoped>
.order-list-page {
  background-color: #f5f5f5;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: red;
  color: white;
  padding: 15px;
}
.title {
  flex: 1;
  text-align: center;
  font-size: 20px;
}
.search-button {
  color: white;
}
.order-status-tabs {
  display: flex;
  background-color: white;
  padding: 10px;
  margin: 0 20px;
}
.order-status-tabs div {
  flex: 1;
  text-align: center;
  padding: 5px 0;
  color: #333;
  cursor: pointer;
}
.order-status-tabs div.active {
  color: red;
  border-bottom: 2px solid red;
}
.order-items {
  margin: 20px;
}
.order-item {
  background-color: white;
  padding: 15px;
  border-radius: 10px;
  margin-bottom: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.status {
  color: red;
}
.order-content {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.order-content img {
  width: 80px;
  height: 80px;
  margin-right: 10px;
}
.product-info {
  flex: 1;
}
.product-info p {
  margin: 0;
}
.price-info {
  text-align: right;
}
.price-info p {
  margin: 0;
}
.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  color: #666;
}
.order-actions {
  text-align: right;
}
</style>